package com.yonyou.iuap.jxzy.controller;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hutool.core.util.StrUtil;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.base.web.BaseController;
import com.yonyou.iuap.jxzy.entity.User;
import com.yonyou.iuap.jxzy.service.RegisterService;
import com.yonyou.iuap.mvc.constants.RequestStatusEnum;


/**
 * @program: jxProject
 * @description: 关于登录和修改密码
 * @author: Pengjx
 * @create: 2019-05-24 13:40
 **/
@Controller
@RequestMapping(value = "/jx_register")
public class RegisterController extends BaseController {

    private Logger logger= LoggerFactory.getLogger(RegisterController.class);

    @Autowired
    private RegisterService service;

    /**
     * 根据账号密码登录
     * @param name
     * @param password
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/get-user",method = {RequestMethod.GET})
    @ResponseBody
    public Object queryUser(@RequestParam String name, @RequestParam String password,@RequestParam String isApp) throws Exception{
        if (StrUtil.isBlank(name) || StrUtil.isBlank(password)){
            return this.buildError("msg", "参数为空!", RequestStatusEnum.FAIL_FIELD);
        }else{
        	JSONObject jsonObject = new JSONObject();
        	try { 
        		User entity= service.findByUser(name,password,isApp);
            	String jsonEntity = jsonObject.toJSONString(entity);
            	JSONObject parseObject = jsonObject.parseObject(jsonEntity);
            	String res = service.getRes(name);
            	res+=",0,2";
            	parseObject.put("responsibility", res);
                return this.buildSuccess(parseObject.toString());
			} catch (Exception e) {
				// TODO: handle exception
				jsonObject.put("message", e.getMessage());
				jsonObject.put("success", "error");
				return jsonObject.toString();
			}
        }

    }

    /**
     * 根据账号修改密码
     * @param password
     * @return
     */
    @RequestMapping(value = "/update-user",method = {RequestMethod.GET})
    @ResponseBody
    public Object updateUser(@RequestParam String name, @RequestParam String password,@RequestParam String isApp){
        if (StrUtil.isBlank(name) || StrUtil.isBlank(password)){
            return this.buildError("msg", "参数为空!", RequestStatusEnum.FAIL_FIELD);
        }else{
        	Integer num=service.updateUserByName(name,password,isApp);
            return this.buildSuccess(num);
        }

    }
    
    /**
     * 根据账号修改取消预约密码 
     * @param orderpassword 取消的预约密码  name 客户姓名
     * @return
     */
    @RequestMapping(value = "/select-order-password",method = {RequestMethod.GET})
    @ResponseBody
    public Object updateByname(@RequestParam String name, @RequestParam String orderpassword){
        if (StrUtil.isBlank(name) || StrUtil.isBlank(orderpassword)){
            return this.buildError("msg", "参数为空!", RequestStatusEnum.FAIL_FIELD);
        }else{
        	Integer num=service.updateOrderPasswordByName(name,orderpassword);
            return this.buildSuccess(num);
        }

    }
}
